package com.black.cat.jvm.classfile.attribute;


import com.black.cat.jvm.classfile.ClassReader;

import java.util.Arrays;

/**
 * @Author blackcat
 * @create 2021/8/11 20:40
 * @version: 1.0
 * @description:
 *
 * LineNumberTable_attribute {
 * u2 attribute_name_index;
 * u4 attribute_length;
 * u2 line_number_table_length;
 * { u2 start_pc;
 * u2 line_number;
 * } line_number_table[line_number_table_length];
 * }
 */
public class LineNumberTableAttribute implements AttributeInfo {

    private LineNumberTableEntry[] lineNumberTable;

    @Override
    public void readInfo(ClassReader reader) {
        int length = reader.nextU2ToInt();

        lineNumberTable = new LineNumberTableEntry[length];

        for (int i = 0; i < length; i++) {
            lineNumberTable[i] = new LineNumberTableEntry(reader);
        }
    }

    private static class LineNumberTableEntry {
        private final int startPc;
        private final int lineNumber;

        public LineNumberTableEntry(ClassReader reader) {
            startPc = reader.nextU2ToInt();
            lineNumber = reader.nextU2ToInt();
        }

        @Override
        public String toString() {
            return "LineNumberTableEntry{" +
                    ",startPc=" + startPc +
                    ", lineNumber=" + lineNumber +
                    "}";
        }

    }

    @Override
    public String toString() {
        return "LineNumberTableAttribute{" +
                "lineNumberTable=" + Arrays.toString(lineNumberTable) +
                "}";
    }

}
